Learn T-SQL Commands with Samples
Skip Navigation Links
Skip Navigation Links.
Expand DatabaseDatabase
Expand TableTable
Expand ViewView
Expand Stored ProcedureStored Procedure
Expand Data FilteringData Filtering
Expand Data GroupingData Grouping
Expand JoinsJoins
Expand TriggerTrigger
Collapse CursorCursor
Expand OperatorsOperators
Expand ConstraintsConstraints
Expand FunctionsFunctions
Expand Conditional ProcessingConditional Processing
Expand LoopingLooping
Expand Error HandlingError Handling
Expand v.IMP Queriesv.IMP Queries
Expand XMLXML
Expand Query PerformanceQuery Performance
Expand QueriesQueries
Expand NormalizationNormalization
Expand CreateCreate
     

Fetch (First, Next, Last, Prior)

 
      DECLARE myCursor CURSOR
      LOCAL
      KEYSET
      FOR SELECT Name FROM EMP

      DECLARE   @fRow   CHAR(10)
      DECLARE   @nRow   CHAR(10)
      DECLARE   @lRow   CHAR(10)
      DECLARE   @pRow   CHAR(10)

      OPEN myCursor

      
---- Retrieve the First row into a variable
FETCH FIRST FROM myCursor INTO @fRow
---- Retrieve the Next row
FETCH NEXT FROM myCursor INTO @nRow
---- Retrieve the Last row into a variable
FETCH LAST FROM myCursor INTO @lRow
---- Retrieve the Previous row
FETCH PRIOR FROM myCursor INTO @pRow SELECT @fRow as 'I am From First Row', @nRow as 'I am From the Next Row', @lRow as 'I am From Last Row', @pRow as 'I am From the Previous row' CLOSE myCursor DEALLOCATE myCursor